package com.hopu.mvc.converter;

import com.hopu.mvc.entity.Student;
import org.springframework.core.convert.converter.Converter;

/**
 * 学生自定义类型转换器
 * 1-张三-20-计算机  == > Student
 */
public class StudentConverter implements Converter<String, Student> {

    @Override
    public Student convert(String s) {
        if(s == null || s.split("-").length != 4){
            throw new RuntimeException("字符串格式有误");
        }
        //将字符串分割转换为Student对象
        String[] strings = s.split("-");
        return new Student(Integer.parseInt(strings[0]),
                strings[1],
                Integer.parseInt(strings[2]),
                strings[3]);
    }
}
